﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteWebUI;
using ThuVien;
using System.Data;
using System.IO;

namespace TranTekBE
{
    public partial class UploadPictures : PagePopUp
    {
        public int ProductISN
        {
            get
            {
                return FunctionsUtils.GetValueIntFromQueryString("isn", 0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Attachments1_AttachmentAdded(object sender, AttachmentItemEventArgs args)
        {

        }

        private int SaveImage(int productISN, int imageFileSize, int isAvatar, string imageFileExt)
        {
            try
            {
                //insert picture
                int PictureISN = (int)FunctionsUtils.Database.ExecuteStoredProcReturnObject("xp_propicture_ins", new string[] { "ProductISN", "FileName", "FileSize" }, new object[] { productISN, string.Empty, imageFileSize });

                //update picture file name
                if (PictureISN > 0)
                    FunctionsUtils.Database.ExecuteQuery(string.Format("Update ProPicture Set [FileName] = '{0}{1}' Where PictureISN = {0}", PictureISN, imageFileExt.ToLower()));

                return PictureISN;
            }
            catch(Exception ex)
            {
                log.WriteLog("Error in UploadPictures (SaveImage): {0} {1}", ex, ex.StackTrace);
                return -1;                
            }            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ltError.Text = string.Empty;

                if (Attachments1.Items.Count > 0)
                {
                    int pictureISN = 0;
                    string imageFileExt = string.Empty;
                    string imageDir = FunctionsUtils.GetConfigValue("ProductImageDir");
                    string productImageDir = Path.Combine(imageDir, this.ProductISN.ToString());

                    if (Directory.Exists(imageDir) == false)
                    {
                        Directory.CreateDirectory(imageDir);
                    }

                    if (Directory.Exists(productImageDir) == false)
                    {
                        Directory.CreateDirectory(productImageDir);
                    }

                    foreach (AttachmentItem item in Attachments1.Items)
                    {
                        imageFileExt = Path.GetExtension(item.FileName);
                        pictureISN = this.SaveImage(this.ProductISN, item.FileSize, 0, imageFileExt);

                        if (pictureISN > 0)
                        {
                            //copy file
                            item.CopyTo(Path.Combine(productImageDir, string.Format("{0}{1}", pictureISN, imageFileExt)));
                        }
                        else
                        {
                            //error
                        }
                    }
                    Attachments1.DeleteAllAttachments();

                    //this.ClosePopupScripManager(true);

                    this.ClosePopupScripManager_CallMethod(string.Format("reloadPage({0}, {1})", 1, this.ProductISN));
                }
            }
            catch (Exception ex)
            {
                ltError.Text = ex.Message;
                log.WriteLog("Error in UploadPictures (Save Upload Image): {0} {1}", ex, ex.StackTrace);
            }
        }

        //private void MoveFile
    }
}
